package spring.r2dbc;

import io.r2dbc.spi.ConnectionFactories;
import io.r2dbc.spi.ConnectionFactory;
import io.r2dbc.spi.ConnectionFactoryOptions;
import io.r2dbc.spi.Option;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

public class R2dbcConnection {
    public static String userDir = System.getProperty("user.dir");


    public static ConnectionFactory getMysql() {


        Map<String, String> options = new HashMap<>();
        options.put("lock_timeout", "10s");
        options.put("statement_timeout", "5m");

        ConnectionFactory connectionFactory = ConnectionFactories.get(ConnectionFactoryOptions.builder()
                .option(ConnectionFactoryOptions.DRIVER, "mariadb")
                .option(ConnectionFactoryOptions.HOST, "127.0.0.1")
                .option(ConnectionFactoryOptions.PORT, 3306)  // optional, defaults to 5432
                .option(ConnectionFactoryOptions.USER, "root")
                .option(ConnectionFactoryOptions.PASSWORD, "123456")
                .option(ConnectionFactoryOptions.DATABASE, "sxf_test")  // optional
//                .option(PostgresqlConnectionFactoryProvider.OPTIONS, options) // optional
                .build());

        return connectionFactory;
    }

    public static ConnectionFactory getPostgresql() {
        //        ConnectionFactory connectionFactory = ConnectionFactories.get("r2dbc:h2:mem:///test?options=DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
//        ConnectionFactory connectionFactory = ConnectionFactories.get("r2dbc:postgresql://127.0.0.1:5432/sxf_test");
        Map<String, String> options = new HashMap<>();
        options.put("lock_timeout", "10s");
        options.put("statement_timeout", "5m");

        ConnectionFactory connectionFactory = ConnectionFactories.get(ConnectionFactoryOptions.builder()
                .option(ConnectionFactoryOptions.DRIVER, "postgresql")
                .option(ConnectionFactoryOptions.HOST, "127.0.0.1")
                .option(ConnectionFactoryOptions.PORT, 5432)  // optional, defaults to 5432
                .option(ConnectionFactoryOptions.USER, "postgres")
                .option(ConnectionFactoryOptions.PASSWORD, "123456")
                .option(ConnectionFactoryOptions.DATABASE, "sxf_test")  // optional
//                .option(PostgresqlConnectionFactoryProvider.OPTIONS, options) // optional
                .option(ConnectionFactoryOptions.LOCK_WAIT_TIMEOUT, Duration.ofSeconds(10))
                .option(ConnectionFactoryOptions.STATEMENT_TIMEOUT, Duration.ofMinutes(5))

                .build());


        return connectionFactory;
    }

    public static ConnectionFactory getH2() {
        ConnectionVO vo = new ConnectionVO();
        //        ConnectionFactory connectionFactory = ConnectionFactories.get("r2dbc:h2:mem:///test?options=DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
//        ConnectionFactory connectionFactory = ConnectionFactories.get("r2dbc:postgresql://127.0.0.1:5432/sxf_test");
        Map<String, String> options = new HashMap<>();
        options.put("lock_timeout", "10s");
        options.put("statement_timeout", "5m");

        ConnectionFactory connectionFactory = ConnectionFactories.get(ConnectionFactoryOptions.builder()
                .option(ConnectionFactoryOptions.DRIVER, "h2")
                .option(ConnectionFactoryOptions.PROTOCOL, "file")
                .option(ConnectionFactoryOptions.DATABASE, userDir + "/wp.h2db")
                .option(ConnectionFactoryOptions.USER, "sa")
//                .option(Option.valueOf("AUTO_SERVER"), "TRUE")
//                .option(Option.valueOf("DB_CLOSE_ON_EXIT"), "FALSE")
                .option(Option.valueOf("DB_CLOSE_DELAY"), "-1")
                .option(Option.valueOf("DATABASE_TO_UPPER"), "false")
                .option(Option.valueOf("MODE"), "PostgreSQL")
//                .option(Option.valueOf("FILE_LOCK"),"SOCKET")

//                .option(PostgresqlConnectionFactoryProvider.OPTIONS, options) // optional
                .option(ConnectionFactoryOptions.LOCK_WAIT_TIMEOUT, Duration.ofSeconds(10))
                .option(ConnectionFactoryOptions.STATEMENT_TIMEOUT, Duration.ofMinutes(5))
                .build());

        return connectionFactory;
    }
}
